package cn.rocky.pandacodegen.controller;

import cn.rocky.pandacodegen.common.R;
import cn.rocky.pandacodegen.domain.dto.DatasourceConfigDTO;
import cn.rocky.pandacodegen.domain.entity.DatasourceConfig;
import cn.rocky.pandacodegen.service.DatasourceConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * 数据源配置控制器
 *
 * @author Rocky
 * @date 2023/06/25
 */
@RestController
@RequestMapping("datasource")
@RequiredArgsConstructor
public class DatasourceConfigController extends BaseController{

    private final DatasourceConfigService datasourceConfigService;

    @RequestMapping("/test")
    public R testDataSource() {
        log.info("===datasource:test=== 接口请求测试:{}","测试接口连通性");
        return new R().ok();
    }

    @RequestMapping("/add")
    public R add(@RequestBody DatasourceConfig datasourceConfig) {
        log.info("=== datasourceConfig ===");
        datasourceConfig.setIsDeleted(0);  // 前端传有问题? 前端暂时解决不了, 故后端解决。一般来说, 该字段数据库给默认值,同时赋初值
        datasourceConfigService.insert(datasourceConfig);
        return new R().ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody DatasourceConfig datasourceConfig) {
        log.info("=== datasourceConfig ===");
        datasourceConfig.setIsDeleted(0);  // 前端传有问题? 前端暂时解决不了, 故后端解决。一般来说, 该字段数据库给默认值,同时赋初值
        datasourceConfigService.update(datasourceConfig);
        return new R().ok();
    }

    @RequestMapping("/list")
    public R list(DatasourceConfigDTO req) {
        log.info("update datasource");
        List<DatasourceConfig> datasourceConfigList = datasourceConfigService.listAll(req);
        return new R().ok(datasourceConfigList);
    }

    @GetMapping("/{id}")
    public R getDatasourceConfigById(@PathVariable Integer id) {
        log.info("find datasource message by id");
        DatasourceConfig datasourceConfig = datasourceConfigService.getById(id);
        return new R().ok(datasourceConfig);
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable Integer id) {
        datasourceConfigService.delete(id);
        return new R().ok();
    }

}
